package ${basePackage}.${service}

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

<#if haveBigDecimal>
import java.math.BigDecimal
</#if>
<#if haveDate>
import java.util.Date
</#if>

import ${basePackage}.${entity}.${className}Entity
import ${basePackage}.vo.${className}VO
import ${basePackage}.pojo.${className}POJO
import ${basePackage}.${mapper}.${className}Mapper
import ${basePackage}.utils.GenerateCodeUtils

/**
 * @描述: ${comment}
 * @作者: ${author}
 * @创建时间: ${createTime}
 * @所属公司: ${company}
 */
@Service
class ${className}Service {

    @Autowired
    lateinit var m${className}Mapper: ${className}Mapper

    /**
      * 查询列表
      *
      * @param param 查询过滤参数
      * @return
      **/
     fun getList(${className}VO param): MutableList${r'<'}${className}VO> {
         var results: MutableList${r'<'}${className}VO>? = m${className}Mapper.select(param)

         return results
     }

     /**
      * 查询列表(仅查询单表字段)
      *
      * @param param 查询过滤参数
      * @return
      **/
      fun getOnlySelf(${className}VO param): MutableList${r'<'}${className}VO> {
          var results: MutableList${r'<'}${className}VO>? = m${className}Mapper.selectOnlySelf(param)

          return results
      }

     /**
      * 根据ID查询数据
      *
      <#list pkColumns as column>
      * @param ${column.attrName} ${column.columnComment}
      </#list>
      * @return
      **/
     fun getById(<#list pkColumns as column>${column.attrName}: ${column.attrType}<#if column_has_next>, </#if></#list>): ${className}VO {
         var result: ${className}VO = m${className}Mapper.selectById(<#list pkColumns as column>${column.attrName}<#if column_has_next>, </#if></#list>)

         return result
     }

     /**
      * 新增数据
      *
      * @param data         待新增数据
      * @param actionUserId  操作用户ID
      * @return
      **/
     fun add(data: ${className}POJO, String actionUserId: String): Int {
        var param: ${className}Entity = ${className}Entity()
         <#list columns as column>
            <#if (pkColumns?size == 1 && column.columnKey == "PRI")>
         param.${column.attrName} = GenerateCodeUtils.generateId()
            <#elseif (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser")>
         param.${column.attrName} = actionUserId
            <#elseif (column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate")>
         param.${column.attrName} = Date()
            <#elseif (column.attrName == "valid")>
         param.${column.attrName} = 1
            <#else>
         param.${column.attrName} = data.${column.attrName}
            </#if>
         </#list>

         val result: Int = m${className}Mapper.insert(param)

         return result
     }

     /**
      * 批量新增
      *
      * @param datas        待新增数据集合
      * @param actionUserId  操作用户ID
      * @return
      **/
     fun addBatch(datas: MutableList${r'<'}${className}POJO>, actionUserId: String): Int {
         var params: MutableList${r'<'}${className}Entity> = mutableListOf()
         var param: ${className}Entity?
         for(${className}POJO data : datas) {
            param = ${className}Entity()
            <#list columns as column>
                <#if (pkColumns?size == 1 && column.columnKey == "PRI")>
            param.${column.attrName} = GenerateCodeUtils.generateId()
                <#elseif (column.attrName == "createBy" || column.attrName =="createUser" || column.attrName == "updateBy" || column.attrName == "updateUser")>
            param.${column.attrName} = actionUserId
                <#elseif (column.attrName == "createAt" || column.attrName == "createTime" || column.attrName == "createDate" || column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate")>
            param.${column.attrName} = Date()
                <#elseif (column.attrName == "valid")>
            param.${column.attrName} = 1
                <#else>
            param.${column.attrName} = data.${column.attrName}
                </#if>
            </#list>

            params.add(param)
         }
         val result: Int = m${className}Mapper.insertBatch(params)

         return result
     }

     /**
      * 更新数据
      *
      * @param data          待更新数据
      * @param actionUserId  操作用户ID
      * @return
      **/
     fun modify(data: ${className}POJO, actionUserId: String): Int {
         var param: ${className}Entity = ${className}Entity()
         <#list columns as column>
            <#if (column.attrName == "updateBy" || column.attrName == "updateUser")>
         param.${column.attrName} = actionUserId
            <#elseif (column.attrName == "updateAt" || column.attrName == "updateTime" || column.attrName == "updateDate")>
         param.${column.attrName} = Date()
            <#elseif (column.attrName == "valid")>
         param.${column.attrName} = 1
            <#else>
         param.${column.attrName} = data.${column.attrName}
            </#if>
         </#list>

         val result: Int = m${className}Mapper.update(param)

         return result
     }

     /**
      * 逻辑删除
      *
      <#list pkColumns as column>
      * @param ${column.attrName} ${column.columnComment}
      </#list>
      * @param actionUserId 操作用户ID
      * @return
      **/
     fun delete(<#list pkColumns as column>${column.attrName}: ${column.attrType}, </#list>actionUserId: String): Int {
         val result: Int = m${className}Mapper.delete(<#list pkColumns as column>${column.attrName}, </#list> actionUserId)

         return result
     }
}